#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <string.h>
#include <pthread.h>

#include <glib/gkeyfile.h>
#include <gtk/gtk.h>
#include <libgnomevfs/gnome-vfs-mime-handlers.h>
#include <libgnomevfs/gnome-vfs-utils.h>
#include <libnautilus-extension/nautilus-extension-types.h>
#include <libnautilus-extension/nautilus-info-provider.h>
#include <libnautilus-extension/nautilus-menu-provider.h>

typedef struct {
    GObject parent;
} SVNTilus;

typedef struct {
    GObjectClass parent_class;
} SVNTilusClass;

static GType svntilus_type;

static NautilusOperationResult
svntilus_update_file_info (NautilusInfoProvider *provider,
			   NautilusFileInfo *file,
			   GClosure *update_complete,
			   NautilusOperationHandle **handle);

static void
svntilus_init (SVNTilus *self)
{
}

static void
svntilus_class_init (SVNTilusClass *class)
{
}

static void
svntilus_info_provider_iface_init (NautilusInfoProviderIface *iface)
{
  iface->update_file_info = svntilus_update_file_info;
}

static void
svntilus_register_type (GTypeModule *module)
{
  static const GTypeInfo info = {
    sizeof (SVNTilusClass),
    (GBaseInitFunc) NULL,
    (GBaseFinalizeFunc) NULL,
    (GClassInitFunc) svntilus_class_init,
    NULL,
    NULL,
    sizeof (SVNTilus),
    0,
    NULL,
  };

  static const GInterfaceInfo info_provider_iface_info = {
    (GInterfaceInitFunc) svntilus_info_provider_iface_init,
    NULL,
    NULL
  };

  svntilus_type = g_type_module_register_type (module,
					       G_TYPE_OBJECT,
					       "SVNTilusExtension",
					       &info, 0);

  g_type_module_add_interface (module,
			       svntilus_type,
			       NAUTILUS_TYPE_INFO_PROVIDER,
			       &info_provider_iface_info);
}

GType
svntilus_get_type (void)
{
  return svntilus_type;
}

static GType provider_types[1];

void
nautilus_module_initialize (GTypeModule  *module)
{
  svntilus_register_type (module);

  provider_types[0] = svntilus_get_type ();
}

void
nautilus_module_shutdown (void)
{
}

void
nautilus_module_list_types (const GType **types,
			    int *num_types)
{
  *types = provider_types;
  *num_types = G_N_ELEMENTS (provider_types);
}

/* actual code */
static NautilusOperationResult
svntilus_update_file_info (NautilusInfoProvider *provider,
			   NautilusFileInfo *file,
			   GClosure *update_complete,
			   NautilusOperationHandle **handle)
{
  GnomeVFSURI *uri;
  gchar *text_uri = nautilus_file_info_get_uri (file);
  uri = gnome_vfs_uri_new (text_uri);

  if (gnome_vfs_uri_is_local (uri) && nautilus_file_info_is_directory (file))
    {
      const gchar *dir_path = gnome_vfs_uri_get_path (uri);
      gchar *full_path = g_strdup_printf ("%s/.svn", dir_path);
      if (g_file_test (full_path, G_FILE_TEST_EXISTS))
	nautilus_file_info_add_emblem (file,
				       "debian");
      g_free (full_path);
    }

  g_free (uri);

  return NAUTILUS_OPERATION_COMPLETE;
}
